// components/banner/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    height: {
      type: Number,
      value: 300
    },
    key: {
      type: String,
      value: 'home'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    banners: [],
    autoplay: true,
    interval: 3000,
    duration: 500,
    bannerSize: 0,
    bannerHeight: 0,
    radius:0,
    showDot:false
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      self = this;
      this.init();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  observers: {
    'height': function (height) {
      // 在 numberA 或者 numberB 被设置时，执行这个函数
      var width = wx.getSystemInfoSync().windowWidth;
      width = width * 0.93 * 0.48;
      console.info("width:" + width);
      console.info("height:" + height);
      this.setData({
        bannerHeight: width
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    init: function (options) {
      //wx.getSystemInfoSync().windowWidth   
      //首页幻灯片
      var params = {};
      params.model = this.properties.key;
      params.needLogin = false;
      var func = this.backAdList.bind(this);
      app.postRequest("/rest/ad/list.htm", params, func);
    },
    backAdList(res) {
      if (res.code == 0) {
        this.setData({
          banners: res.list,
          bannerSize: res.list.length
        });
        var width = wx.getSystemInfoSync().windowWidth;
        width = width * 0.93;
        if (res.rate == null || res.rate == undefined) {
          res.rate = 2;
        }
        var height = width / res.rate;
        height=Math.ceil(height);
        this.setData({
          bannerHeight: height
        });
        console.info(res.radius);
        if (res.radius == null || res.radius == undefined) {
          res.radius = 2;
        }
        this.setData({
          radius: res.radius
        });

        if(res.list.length>1){
          this.setData({
            showDot: true
          });
          
        }

      }
    },
    tapBanner(event) {
      var object = event.currentTarget.dataset.object;
      if (object.url && object.url.length > 5) {
        wx.navigateTo({
          url: object.url,
        })
      }
    }
  }
})